<template>
  <div class="addclassify">
    <el-tabs type="border-card">
      <el-tab-pane>
        <span slot="label"><i class="el-icon-date"></i> 我的行程</span>
        <el-form
          :model="ruleForm"
          status-icon
          :rules="rules"
          ref="ruleForm"
          label-width="100px"
          class="demo-ruleForm"
        >
          <el-form-item label="试题类型名称" prop="textname">
            <el-input v-model="ruleForm.name"></el-input>
          </el-form-item>
          <el-form-item label="试题序号" prop="textsort">
            <el-input v-model.number="ruleForm.sort"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="submitForm('ruleForm')"
              >提交</el-button
            >
            <el-button @click="resetForm('ruleForm')">重置</el-button>
          </el-form-item>
        </el-form>
      </el-tab-pane>
      <el-tab-pane label="消息中心">消息中心</el-tab-pane>
      <el-tab-pane label="角色管理">角色管理</el-tab-pane>
      <el-tab-pane label="定时任务补偿">定时任务补偿</el-tab-pane>
    </el-tabs>
  </div>
</template>

<script>
import { addClassify } from "../../../api/classify";

export default {
  data() {
    var checkName = (rule, value, callback) => {
      if (!value) {
        return callback(new Error("内容不能为空"));
      } else {
        callback();
      }
    };
    var checkSort = (rule, value, callback) => {
      if (!value) {
        return callback(new Error("内容不能为空"));
      }
      setTimeout(() => {
        if (!Number.isInteger(value)) {
          callback(new Error("请输入数字值"));
        } else {
          callback();
        }
      }, 1000);
    };

    return {
      ruleForm: {
        sort: "",
        name: "",
      },
      rules: {
        name: [{ validator: checkName, trigger: "blur" }],
        sort: [{ validator: checkSort, trigger: "blur" }],
      },
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          addClassify({
            text: this.ruleForm.name,
            sort: this.ruleForm.sort,
          }).then((res) => {
            if (res.data.msg === "数据插入") {
              this.ruleForm.name = "";
              this.ruleForm.sort = "";
              this.$message({
                message: "试题类型添加成功",
                type: "success",
              });
            }else{
                this.$message({
                message: "试题类型添加失败",
                type: "error",
              });
            }
          });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
  },
};
</script>

<style>
</style>